package ParamTypeTransfer;


import com.google.gson.Gson;
import lombok.Data;

import java.math.BigDecimal;

public class Test1 {

    /**
     * java
     * 基本数据类型 是值传递
     * 引用数据类型才是 地址传递
     * 即
     *      基本数据类型 并不会改变
     *      引用数据类型  是指向内存中的地址 会改变
     * @param args
     */

    public static void main(String[] args) {
        System.out.println("test start ...");
        
        int a = 9;
        int result = calculate(a);
        System.out.println("param a: "+a);
        System.out.println("after calculate a: "+a);
        System.out.println("calculate result: "+result);

        System.out.println("---------- calculate test over ----------");


        Gson gson = new Gson();

        User user = new User();
        user.setUserId(1222L);
        user.setUserName("bob");
        System.out.println("before modify user:"+gson.toJson(user));
        User user1 = modifyUser(user);
        System.out.println("after modify user:"+gson.toJson(user1));

        System.out.println("---------- Entity test over ----------");




        int p = 1;
        int q = p++;
        System.out.println("q value: "+q);

        int m = ++p;
        System.out.println("m value "+m);
        System.out.println("---------- ++ test over ----------");


        // Integer 范围[-128,127] 在这个范围内的数据会直接从栈中取数据 是同一个地址  超出的数值 会 进行new Integer
        // 会在堆中产生新的对象 地址不同 用等号比较 肯定是false
        Integer i1 = 988;
        Integer i2 = 988;
        System.out.println(i1==i2);
        System.out.println("----------  test Integer over ----------");


        // this is false option
        BigDecimal bigDecimal = new BigDecimal(0.1);
        System.out.println(bigDecimal.toString());

        // these two are right
        BigDecimal bigDecimal11 = BigDecimal.valueOf(0.1);
        System.out.println(bigDecimal11);
        BigDecimal bigDecimal2 = new BigDecimal("0.1");
        System.out.println(bigDecimal2);
        System.out.println("----------  test BigDecimal over ----------");


        // (n-1)&m = m%n
        String s1 = "rtt";
        int hashCode = s1.hashCode();
        long time = System.nanoTime();
        int index = hashCode%8;
        System.out.println("% 运算 time: "+ (System.nanoTime()-time));
        time = System.nanoTime();
        int index1 = (8-1)&hashCode;
        System.out.println("& 运算 time: "+ (System.nanoTime()-time));
        System.out.println(index == index1);
        System.out.println("test Hash");

        System.out.println("test end ...");
    }


    private static User modifyUser(User user) {
        Gson gson = new Gson();
        System.out.println("param user info:"+gson.toJson(user));
        user.setUserId(22222L);
        user.setUserName("tt");
        System.out.println("modify user info:"+gson.toJson(user));
        return user;
    }


    private static int calculate(int a) {
        System.out.println("calculate param :" + a);
        a = a+1;
        System.out.println("after calculate param value: "+a);
        return a;
    }




    @Data
    static class User{
        private Long userId;
        private String userName;

        public User() {
        }

        public User(Long userId, String userName) {
            this.userId = userId;
            this.userName = userName;
        }
    }

}
